package models

import (

	"gopkg.in/mgo.v2/bson"
	"kukukaki/db"
 
)


type UserInfo struct {
	Openid     string `json:"openid" bson:"_id"`      //用户唯一标识
	SessionKey string `json:"session_key" bson:"session_key"` //会话密钥
	Unionid    string `json:"unionid"  bson:"unionid"`     //用户在开放平台的唯一标识符，在满足 UnionID 下发条件的情况下会返回，详见 UnionID 机制说明。
	NickName   string `json:"nickName"  bson:"nickName"`
	AvatarUrl  string `json:"avatarUrl"  bson:"avatarUrl"`
	City       string `json:"city"  bson:"city"`
	Country    string `json:"country"  bson:"country"`
	Province   string `json:"province"  bson:"province"`
	Gender     int    `json:"gender"  bson:"gender"`
	UseNumber   int    `json:"useNumber"  bson:"useNumber"`
	RiskWarningNumber int   `json:"riskWarningNumber"  bson:"riskWarningNumber"`
	LastUseTime  string     `json:"lastUseTime"  bson:"lastUseTime"`
	LastShareTime  string   `json:"lastShareTime"  bson:"lastShareTime"`
	RegisterTime string   `json:"registerTime"  bson:"registerTime"`
	LastLoginTime  string   `json:"lastLoginTime"  bson:"lastLoginTime"`
	ShareNumber int         `json:"shareNumber"  bson:"shareNumber"`
	TheDayUseAmount  int    `json:"theDayUseAmount"  bson:"theDayUseAmount"`
	
	
}


func QueryUser(u *UserInfo) (err error) {
	con := db.GetDataBase().C("User")
	err = con.Find(bson.M{"_id":  u.Openid}).One(u)
	return err 
}

func UpdateLoginTime(u *UserInfo) (err error) {
        con := db.GetDataBase().C("User")
        err = con.Update(bson.M{"_id": u.Openid},bson.M{"$set": bson.M{"lastLoginTime":u.LastLoginTime}})
        return err
}

func UpdateUserUse(u *UserInfo) (err error) {
        con := db.GetDataBase().C("User")
        err = con.Update(bson.M{"_id": u.Openid},bson.M{"$set": bson.M{"useNumber":u.UseNumber,"riskWarningNumber":u.RiskWarningNumber,"theDayUseAmount":u.TheDayUseAmount}})
        return err
}

func UpdateShareNum(u *UserInfo) (err error) {
        con := db.GetDataBase().C("User")
        err = con.Update(bson.M{"_id": u.Openid},bson.M{"$set": bson.M{"shareNumber":u.ShareNumber}})
        return err
}

func UpdateUseNum(u *UserInfo) (err error) {
        con := db.GetDataBase().C("User")
        err = con.Update(bson.M{"_id": u.Openid},bson.M{"$set": bson.M{"theDayUseAmount":u.TheDayUseAmount}})
        return err
}

func UpdateUser(u *UserInfo) (err error) {
        con := db.GetDataBase().C("User")
        //bson.M
         //更新
        err = con.Update(bson.M{"_id": u.Openid},bson.M{"$set": u})
        
        return err
}

//func Register( wechatname string,childnickname string,childbirthday string,city string,address string,phone int,gps []string ) (err error) {
func Register(u *UserInfo) (err error) {
    con := db.GetDataBase().C("User")
    
    err = con.Insert(u)
    
    return err
}